package com.c611.smartclassroom.controller;

import com.c611.smartclassroom.common.DataGridView;
import com.c611.smartclassroom.model.Build;
import com.c611.smartclassroom.model.WorkOrder;
import com.c611.smartclassroom.service.BuildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/build")
public class BuildControler {

    @Autowired
    BuildService buildService;

    /**
     * 根据学校ID，分页查询所有教学楼
     * xqr 或根据学校ID和教学楼名称查询教学楼,tbuildname可加可不加
     * @param schoolid
     * @param page
     * @param pageSize
     * @return
     */
    //localhost/build/queryAllBuild?schoolid=201&page=1&pageSize=3
    @PostMapping("/queryAllBuild")
    public DataGridView<Build> queryAllBuild(@RequestParam("schoolid") String schoolid,@RequestParam(value = "tbuildname",required = false) String tbuildname,
                                             @RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize){

        if(schoolid==null) return null;
        //查询页数是否合理
        if (page < 1 || pageSize < 1) return null;
        //通过schoolId查询数据库中的教学楼表中的教学楼总数
        long buildCount = buildService.queryAllBuildCount(schoolid);

        //计算该页是否为空（前一页的记录数都大于数据库总数）
        if ((page - 1) * pageSize >= buildCount) return null;
        //参数正确，执行分页查询，返回构造的json数据
        return new DataGridView<Build>(0,"ok",buildCount,buildService.queryAllBuild(schoolid,tbuildname,page,pageSize));
//        return buildService.queryAllBuild(schoolid,page,pageSize);
    }

    /**
     * 根据教学楼ID，对教学楼信息进行编辑
     * @param build
     * @return
     */
    //localhost/build/saveBuild?tbuildid=1&schoolid=12
    @PostMapping("/saveBuild")
    public int saveBuild(Build build) {
        if(build==null) return 0;
        return buildService.saveBuild(build);
    }

    /**
     * 向t_built表中添加新的教学楼
     * @param build
     * @return
     */
    //localhost/build/addBuild?tbuildid=7&schoolid=13&tbuildname=C栋&delflag=0
    @PostMapping("/addBuild")
    public int addBuild(Build build){
        if(build== null) return 0;
        if(build.getSchoolid() == null || build.getTbuildid() == null || build.getTbuildname() == null) return 0;
        if(build.getTbuildid().length() <= 3 || build.getTbuildid().length() > 5) return 3;
        if(!build.getSchoolid().equals(build.getTbuildid().substring(0,3))) return 3;
        //先判断id是否重复
        int flag = buildService.checkBuildId(build.getTbuildid());
        if(flag == 1) return 2;
        return buildService.addBuild(build);
    }

    /**
     * 根据教学楼ID，删除教学楼
     * @param tbuildid
     * @return
     */
    //localhost/build/delBuild?tbuildid=1
    @PostMapping("/delBuild")
    public int delBuild(@RequestParam("tbuildid") String tbuildid){
        if(tbuildid== null) return 0;
        return buildService.delBuild(tbuildid);
    }
}
